package net.csiit.tools.fileserver.common.util;

import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URLEncoder;
import java.util.Map;

/**
 * 下载预览工具类
 * @author gl
 */
@Slf4j
public class HttpResponseUtil {
    public static final Map<String, String> MEDIA_FORMATES = Maps.newHashMap();

    static {
        MEDIA_FORMATES.put("mp4", "video/mp4");
        MEDIA_FORMATES.put("avi", "video/avi");
        MEDIA_FORMATES.put("mpeg", "video/mpg");
        MEDIA_FORMATES.put("mpg", "video/mpg");

        MEDIA_FORMATES.put("mp3", "audio/mp3");
        MEDIA_FORMATES.put("wav", "audio/wav");

        MEDIA_FORMATES.put("jpg", "image/jpeg");
        MEDIA_FORMATES.put("jpeg", "image/jpeg");
        MEDIA_FORMATES.put("png", "image/png");
        MEDIA_FORMATES.put("tiff", "image/tiff");
        MEDIA_FORMATES.put("tif", "image/tiff");
        MEDIA_FORMATES.put("gif", "image/gif");
        MEDIA_FORMATES.put("bmp", "image/bmp");
    }

    public static void send404(HttpServletResponse response) {
        response.setStatus(404);
    }

    /**
     * 预览图片
     *
     * @param response   相应
     * @param inputStream      图片的file对象
     * @param fileFormat 文件格式 比如 png jpg
     * @throws IOException 异常
     */
    public static void previewImage(HttpServletResponse response, InputStream inputStream, String fileFormat) throws IOException {
        try (OutputStream os = response.getOutputStream()) {
            response.setContentType("image/" + fileFormat);
            BufferedImage image1 = ImageIO.read(inputStream);
            ImageIO.write(image1, fileFormat, os);
        }
    }


    public static void sendFileAttachment(HttpServletResponse response, File file, String fileName) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
        try (InputStream is = new FileInputStream(file);
             OutputStream os = response.getOutputStream();
        ) {
            IOUtils.copy(is, os);
        }
    }

    public static void sendFileAttachment(HttpServletResponse response, InputStream inputStream, String fileName) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
        try (OutputStream os = response.getOutputStream()) {
            IOUtils.copy(inputStream, os);
        }
    }

    public static void sendFileStream(HttpServletResponse response, File file, String fileName) throws IOException {
        String postfix = fileName.substring(fileName.lastIndexOf(".") + 1);
        String contentType = MEDIA_FORMATES.get(StringUtils.lowerCase(postfix));
        if (!StringUtils.isEmpty(contentType)) {
            response.setContentType(contentType);
            try (OutputStream os = response.getOutputStream();
                InputStream inputStream = new FileInputStream(file)) {
                IOUtils.copy(inputStream, os);
            }
        } else {
            sendFileAttachment(response, file, fileName);
        }
    }

    public static void sendFileStream(HttpServletResponse response, InputStream inputStream, String fileName) throws IOException {
        String postfix = fileName.substring(fileName.lastIndexOf(".") + 1);
        String contentType = MEDIA_FORMATES.get(StringUtils.lowerCase(postfix));
        if (!StringUtils.isEmpty(contentType)) {
            response.setContentType(contentType);
            try (OutputStream os = response.getOutputStream()) {
                IOUtils.copy(inputStream, os);
            }
        } else {
            sendFileAttachment(response, inputStream, fileName);
        }
    }

}
